package model;

import java.util.*;
import java.io.Serializable;

public class TockUsers implements Serializable {
	private static final long serialVersionUID = 0; // What the hell is this for??
	
	private ArrayList<String> chatters;
	private String[] players;
	private int nextPlayer;
	private int dealer;
	
	public TockUsers() {
		chatters = new ArrayList<String>();
		players = new String[4];
		for (int i=0; i<players.length; i++)
			players[i] = "";
		nextPlayer = 0;
		dealer = 0;
	}
	
	public void newChatter(String person) {if (!chatters.contains(person)) chatters.add(person);}
	public void removeChatter(String person) {chatters.remove(person);}
	
	public ArrayList<String> getChatters() { return chatters; }
	public String[] getPlayers() { return players; }
	public String getPlayer(int num) {
		int count = 0;
		for (int i=0; i<players.length; i++) {
			if (!players[i].equals("")) {
				if (count == num) return players[i];
				else count++;
			}
		}
		return "";
	}
	
	public int getNextPlayer() {return nextPlayer;}
	public int getDealer() {return dealer;}
	
	public void setNextPlayer(int person) {nextPlayer = person;}
	public void setDealer(int person) {dealer = person;}
	
	public void newPlayer1(String person) { newPlayer(person, 0); }
	public void newPlayer2(String person) { newPlayer(person, 1); }
	public void newPlayer3(String person) { newPlayer(person, 2); }
	public void newPlayer4(String person) { newPlayer(person, 3); }
	public void newPlayer(String person, int spot) {
		removeChatter(person);
		players[spot] = person;
	}
	
	
	public void removePlayer(int player) {
		chatters.add(players[player]);
		players[player] = "";
	}
	
	public void removePlayer(String sender) {
		if (sender.equals(players[0])) removePlayer(0);
		if (sender.equals(players[1])) removePlayer(1);
		if (sender.equals(players[2])) removePlayer(2);
		if (sender.equals(players[3])) removePlayer(3);
	}
	
	public boolean isPlaying(String person) {
		for (int i=0; i<4; i++) {
			if (players[i].equals(person)) return true;
		}
		return false;
	}
	
	public int numOfPlayers() {
		int count = 0;
		for (int i=0; i<4; i++) {
			if (!(players[i].equals(""))) count++;
		}
		return count;
	}
	
	public int getPlayerIndex(String player) {
		int count = -1;
		for (int i=0; i<players.length; i++) {
			if (!(players[i].equals(""))) count++;
			if (players[i].equals(player)) return count;
		}
		return -1;
	}
}
